package de.koller.worlders.engine.tiles.light;

import de.koller.oceanFramework.util.ColorIntUtil;

public class LightUtil {
	public static final float LIGHT_DISPOSE = 9; 
	
	public static int maxDif( int src, int dst ) {
		int m = 0;
		m = Math.max( ColorIntUtil.getRValue( dst ) - ColorIntUtil.getRValue( src ), m );
		m = Math.max( ColorIntUtil.getGValue( dst ) - ColorIntUtil.getGValue( src ), m );
		m = Math.max( ColorIntUtil.getBValue( dst ) - ColorIntUtil.getBValue( src ), m );
		return m;
	}
	
	public static float distanceDif( int src, int dst ) {
		int md = maxDif( src, dst );
		return (float)md / LIGHT_DISPOSE;
	}
	
	public static int subLight( int dst, int steps ) {
		int v = (int)(steps * LIGHT_DISPOSE);
		int r = ColorIntUtil.getRValue( dst ) - v;
		int g = ColorIntUtil.getGValue( dst ) - v;
		int b = ColorIntUtil.getBValue( dst ) - v;
		if( r < 0 ) r = 0;
		if( g < 0 ) g = 0;
		if( b < 0 ) b = 0;
		return ColorIntUtil.getIntColor( 0xff, r, g, b );
	}
	
	public static boolean isMeBrighter( int me, int other ) {
		int r1 = ColorIntUtil.getRValue( me );
		int r2 = ColorIntUtil.getRValue( other );
		if( r1 > r2 ) return true;
		
		int g1 = ColorIntUtil.getGValue( me );
		int g2 = ColorIntUtil.getGValue( other );
		if( g1 > g2 ) return true;
		
		int b1 = ColorIntUtil.getBValue( me );
		int b2 = ColorIntUtil.getBValue( other );
		if( b1 > b2 ) return true;
		
		return false;
	}
	
	public static int subLight( int light, float multiply ) {
		int v = (int)(LIGHT_DISPOSE*multiply);
		int lr = ColorIntUtil.getRValue( light ) - v; if( lr < 0 ) lr = 0;
		int lg = ColorIntUtil.getGValue( light ) - v; if( lg < 0 ) lg = 0;
		int lb = ColorIntUtil.getBValue( light ) - v; if( lb < 0 ) lb = 0;

		return ColorIntUtil.getIntColor( 0xff, lr, lg, lb );
	}
	
	public static int mergeMax( int l1, int l2 ) {
		int r1 = ColorIntUtil.getRValue( l1 );
		int g1 = ColorIntUtil.getGValue( l1 );
		int b1 = ColorIntUtil.getBValue( l1 );

		int r2 = ColorIntUtil.getRValue( l2 );
		int g2 = ColorIntUtil.getGValue( l2 );
		int b2 = ColorIntUtil.getBValue( l2 );

		return ColorIntUtil.getIntColor(	0xff,
											Math.max( r1, r2 ),
											Math.max( g1, g2 ),
											Math.max( b1, b2 ) );
	}

	public static float maxDistance( int src ) {
		return (float)maxRGB( src ) / LIGHT_DISPOSE;
	} 
	
	public static int maxRGB( int src ) {
		int m = 0;
		m = Math.max( m, 0xff - ColorIntUtil.getRValue( src ) );
		m = Math.max( m, 0xff - ColorIntUtil.getGValue( src ) );
		m = Math.max( m, 0xff - ColorIntUtil.getBValue( src ) );
		return m;
	}
	
}
